package com.shoppingmall.login;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.ToastUtil;
import com.shoppingmall.R;
import com.shoppingmall.bean.RegisterBean;
import com.shoppingmall.bean.SmsCodeBean;
import com.shoppingmall.data.DataApi;
import com.shoppingmall.interfaces.CallBackId;
import com.shoppingmall.retrofitUtils.RetrofitHelp;
import com.shoppingmall.widget.RegisterDialog;
import com.shoppingmall.widget.TimeTextLogin;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.image_login)
    ImageView imageLogin;
    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.re_username)
    RelativeLayout reUsername;
    @BindView(R.id.btn_get_code)
    Button btnGetCode;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.re_password)
    RelativeLayout rePassword;
    @BindView(R.id.invitation_code)
    EditText invitationCode;
    @BindView(R.id.re_invitation_code)
    RelativeLayout reInvitationCode;
    @BindView(R.id.bnt_register)
    Button bntRegister;
    @BindView(R.id.back_login)
    TextView backLogin;
    @BindView(R.id.eroormessgae)
    TextView eroormessgae;

    private TimeTextLogin mTimeText;

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
//        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0,null);
    }

    @Override
    protected void initEvent() {

    }


    @OnClick({R.id.username, R.id.re_username, R.id.btn_get_code, R.id.password, R.id.re_password, R.id.invitation_code, R.id.bnt_register, R.id.back_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.username:
                break;
            case R.id.re_username:
                break;
            case R.id.btn_get_code:

                if (EmptyUtil.isEmpty(username.getText().toString())) {
                    ToastUtil.showS(this, "请输入手机号码");
                } else {
                    if (isPhoneNumber(username.getText().toString()) == false) {
                        eroormessgae.setText("请输入正确的手机号");
                        return;
                    }else{
                        eroormessgae.setText("");
                    }
                }

                getsmscode(username.getText().toString());

                break;
            case R.id.password:
                break;
            case R.id.re_password:
                break;
            case R.id.invitation_code:
                break;
            case R.id.bnt_register:

                if (EmptyUtil.isEmpty(username.getText().toString())) {
                    ToastUtil.showS(this, "请输入手机号码");
                } else {
                    if (isPhoneNumber(username.getText().toString()) == false) {
                        eroormessgae.setText("请输入正确的手机号");
                        return;
                    }else{
                        eroormessgae.setText("");
                    }
                }


                if (EmptyUtil.isEmpty(password.getText().toString())){
                    ToastUtil.showS(this, "请输入验证码");
                    return;
                }

               /* if (EmptyUtil.isEmpty(invitationCode.getText().toString())){
                    ToastUtil.showS(this, "请输入邀请码");
                    return;
                }*/

                    toregeister("e10adc3949ba59abbe56e057f20f883e", username.getText().toString(), invitationCode.getText().toString(), password.getText().toString());

                break;
            case R.id.back_login:
                /*Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);*/
                this.finish();
                break;
        }
    }


    /**
     * 注册
     */
    public void toregeister(String password, String mobile, String invitation_code, String sms_code) {
        RxHttpUtils.createApi(DataApi.class)
                .register(password, mobile, invitation_code, sms_code)
                .compose(Transformer.<RegisterBean>switchSchedulers())
                .subscribe(new CommonObserver<RegisterBean>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(RegisterBean registerBean) {
                        RegisterDialog registerDialog = new RegisterDialog();
                        registerDialog.setSelectAddresFinish(new CallBackId() {
                            @Override
                            public void getid(String id, String name) {
                                RegisterActivity.this.finish();
                            }
                        });
                        registerDialog.show(getSupportFragmentManager(), "");
                    }
                });
    }

    /**
     * 获取验证码
     * 两种请求都是retrofit 一种是用的 RxHttpUtils框架 另外一种是自己写的 RetrofitHelp工具类
     */

    public  void getsmscode(String phone){
       /* DataApi  dataApi = RetrofitHelp.getInstance().getRetrofit().create(DataApi.class);
        dataApi.smscode(phone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            String body=responseBody.string();
                            ToastUtil.showS(RegisterActivity.this, body);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showS(RegisterActivity.this, e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });*/

        RxHttpUtils.createApi(DataApi.class)
                .smscode(phone)
                .compose(Transformer.<ResponseBody>switchSchedulers())
                .subscribe(new CommonObserver<ResponseBody>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(ResponseBody smsCodeBean) {
                        mTimeText = new TimeTextLogin(60000, 1000, btnGetCode);
                        mTimeText.start();
                    }
                });
    }




}
